<?php

namespace app\common\model;

use think\Model;

class AfterItem extends Model
{
	// 表名
	protected $name = 'after_item';
	
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = true;
	protected $updateTime = null;
	
	// 追加属性
	protected $append = [
		'role_text',
	];
	
	const ROLE_USER = 1;
	const ROLE_BUS = 2;
	const ROLE_SYS = 3;
	
	public static $_role = [
		self::ROLE_USER => '买家',
		self::ROLE_BUS => '卖家',
		self::ROLE_SYS => '平台'
	];
	
	public function getRoleTextAttr($value, $data)
	{
		$value = $value ? $value : $data['role'];
		$list = self::$_role;
		return isset($list[$value]) ? $list[$value] : '';
	}


	public function aftersales(){
		return $this->belongsTo('afterSales', 'after_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加售后详情记录
	 * @param $afterId 售后ID
	 * @param $role 角色
	 * @param $title 标题
	 * @param string $content 内容
	 * @return bool
	 */
	public function addItem($afterId, $role, $title, $content=''){
		$item = [
			'after_id' => $afterId,
			'role' => $role,
			'title' => $title,
			'content' => $content,
		];
		
		$res = $this->isUpdate(false)->save($item);
		if($res) {
			return true;
		} else {
			$this->error = '添加售后详情出错';
			return false;
		}
	}
}
